import React, { Component } from 'react';
import { connect } from 'react-redux';
import { messages } from "share/common";
import { Form, Button, message, Spin, Checkbox } from 'antd';
import autoAuditService from "../auto-audit.service";
import AutoAuditRuleDetailSlide from './auto-audit-rule-slide-head'
import { permission } from 'containers/finance-setting/permisson';

@connect(mapStateToProps)
@permission
class AutoAuditRuleDetail extends Component {

  constructor(props) {
    super(props);
    this.state = {
      saving: false,
      needRefresh: false,
      head: {},
      priority: this.getPriority(props)
    };
  }


  componentDidMount() {
    const { record } = this.props;
    if(record.id) {
      this.setHead(record);
    }
  }

  getPriority = (props) => {
    let priority = {}, { params: { record: { levelCode, levelOrgId, levelOrgName, companies, companyNameList } } } = props;
    if(!levelCode){
      return void 0;
    }
    priority.distribution = {};
    companies && (priority.distribution.companies = companies.map((item, index) => {
      return {
        name: companyNameList[index],
        id: item
      };
    }));
    priority = { ...priority, levelCode, levelOrgId, levelOrgName };
    return priority;
  };

  setHead = head => {

    this.setState({ head }, () => {
      const { setFieldsValue } = this.props.form;
      let valueList = ['enabled', 'ruleCode', 'ruleName', 'priority'];
      let values = this.pick(head, valueList);
      setFieldsValue(values);
    });
  };

  handleBack = () => {
    const { needRefresh } = this.state;
    this.props.close(needRefresh);
  };

  handleSave = e => {
    e.preventDefault();
    const { form } = this.props;
    const { priority } = this.state;
    form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        if (!priority) {
          message.error(messages('expense-14.key366')/*请选择所属组织*/);
          return;
        }
        const { head, priority: { distribution } } = this.state;
        this.setState({ saving: true });
        let params = Object.assign({}, head, values);
        if(distribution && distribution.companies){
          priority.companies = distribution.companies.map(item => item.id)
        }else{
          priority.companies = void 0;
        }
        delete priority.distribution;
        autoAuditService.saveSmartAuditRules({ ...params, ...priority })
          .then(res => {
            message.success(messages('common.operate.success')/*操作成功*/);
            this.props.close(true)
          })
          .finally(() => {
            this.setState({ saving: false });
          });
      }
    });
  };

  handlePriority = priority => {
    this.setState({ priority });
  };

  render() {
    const { form, noCompany } = this.props;
    const { saving, head, head: { readOnly }, priority } = this.state;
    return (
      <div className='auto-audit-setting'>
        <Form className='new-form' onSubmit={this.handleSave}>
          <AutoAuditRuleDetailSlide
            disabled={this.isReadOnly() || readOnly}
            priority={priority}
            onChange={this.handlePriority}
            form={form}
            head={head}
            noCompany={noCompany}
          />
          <div className='slide-footer'>
            <Button
              type='primary'
              htmlType='submit'
              loading={saving}
              disabled={this.isReadOnly() || readOnly}
            >
              {messages('common.save')/*保存*/}
            </Button>
            <Button onClick={this.handleBack}>{messages('common.back')/*返回*/}</Button>
          </div>
        </Form>
      </div>
    )
  }
}

function mapStateToProps(state) {
  return {
    currentMenuPermission: state.main.currentMenuPermission,
  };
}

const WrappedAutoAuditRuleDetail = Form.create()(AutoAuditRuleDetail);

export default WrappedAutoAuditRuleDetail

